package vip.xiaonuo.client.modular.common;

import cn.hutool.core.io.IoUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.cache.CommonCacheOperator;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.dev.api.DevConfigApi;
import vip.xiaonuo.dev.modular.file.param.DevFileIdParam;
import vip.xiaonuo.dev.modular.file.service.DevFileService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;

/**
 * 文件控制器
 *
 * @date 2022/2/23 18:26
 **/
@Api(tags = "文件控制器")
@ApiSupport
@RestController
@Validated
public class ClientFileController {

    /**
     * 默认文件引擎
     */
    private static final String SNOWY_SYS_DEFAULT_FILE_ENGINE_KEY = "SNOWY_SYS_DEFAULT_FILE_ENGINE";

    @Resource
    private DevConfigApi devConfigApi;

    @Resource
    private DevFileService devFileService;
    @Resource
    private CommonCacheOperator commonCacheOperator;

    /**
     * 动态上传文件返回id
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @ApiOperationSupport(order = 1)
    @ApiOperation("动态上传文件返回id")
    @CommonLog("动态上传文件返回id")
    @PostMapping("/client/file/upload")
    public CommonResult<String> uploadDynamicReturnId(@RequestPart("file") MultipartFile file) {
        return CommonResult.data(devFileService.uploadReturnUrl(devConfigApi.getValueByKey(SNOWY_SYS_DEFAULT_FILE_ENGINE_KEY), file));
    }

    /**
     * 复制下载链接到redis
     *
     * @param devFileIdParam 文件id
     * @return redis中的url
     */
    @ApiOperationSupport(order = 16)
    @ApiOperation("复制下载链接到redis")
    @CommonLog("复制下载链接到redis")
    @GetMapping("/client/file/copy_download_url")
    public CommonResult<String> copyDownloadUrl(@Valid DevFileIdParam devFileIdParam) {
        return CommonResult.data(devFileService.copyDownloadUrl(devFileIdParam.getId()));
    }

    /**
     * 根据url path参数下载文件
     *
     * @param randomKey 唯一参数
     */
    @ApiOperationSupport(order = 17)
    @ApiOperation("根据url path参数下载文件")
    @CommonLog("根据url path参数下载文件")
    @GetMapping(value = "/client/file/download/{randomKey}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadByPath(@PathVariable("randomKey") @Valid @NotEmpty(message = "参数不能为空") String randomKey, HttpServletResponse response) throws IOException {
        String key = String.format("temp_download_file_id:%s", randomKey);
        Object object = commonCacheOperator.get(key);
        if (object == null) {
            response.setHeader("Content-Type", "text/plain;charset=utf-8");
            IoUtil.writeUtf8(response.getOutputStream(), true, "文件下载链接已失效，请重新复制");
            return;
        }
        DevFileIdParam param = new DevFileIdParam();
        param.setId(object.toString());
        devFileService.download(param, response);
    }
}
